package com.css.fxfzysx.modules.ysxManagement.repository.impl;

import com.css.fxfzysx.modules.ysxManagement.entity.BdFactorEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.DiBoundEntity;
import com.css.fxfzysx.modules.ysxManagement.repository.BdFactorRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Repository
public class BdFactorRepositoryImpl implements BdFactorRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void saveBdFactor(BdFactorEntity it) {
        jdbcTemplate.update("insert into czt_bridge_damage_factor(cbdf_id,serial_number,category,coefficient_value,create_user,create_time,del_flag,damage_factor,default_data) " +
                "values(?,?,?,?,?,?,?,?,?)", it.getCbdfId(), it.getSerialNumber(), it.getCategory(), it.getCoefficientValue(), it.getCreateUser(), it.getCreateTime(), it.getDelFlag(),it.getDamageFactor(),it.getDefaultData());
    }

    @Override
    public void updateBdFactor(BdFactorEntity it) {
        String sql = "update czt_bridge_damage_factor set serial_number= :serialNumber,category= :category,coefficient_value= :coefficientValue,update_user= :updateUser,update_time= :updateTime,damage_factor= :damageFactor" +
                " where cbdf_id= '"+it.getCbdfId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<BdFactorEntity> getAllBdFactor() {
        String sql = "select * from czt_bridge_damage_factor where del_flag='0' order by create_time asc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(BdFactorEntity.class));
    }

    @Override
    public boolean deleteBdFactorById(String bdFactorId) {
        String sql = "update czt_bridge_damage_factor set del_flag = '1' where cbdf_id=?";
        return jdbcTemplate.update(sql, bdFactorId) > 0;
    }

    @Override
    public BigDecimal getBdFactor(String damageFactor, String category) {
        try {
            String sql = "select * from czt_bridge_damage_factor where del_flag = '0' and damage_factor='"+damageFactor+"' and category='"+category+"'";
            BdFactorEntity bdFactorEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(BdFactorEntity.class));
            return bdFactorEntity.getCoefficientValue();
        } catch (DataAccessException e) {
            return new BigDecimal(0);
        }
    }

    @Override
    public List<BdFactorEntity> getBdById(String cbdfId) {
        String sql = "select * from czt_bridge_damage_factor where cbdf_id='"+cbdfId+"'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(BdFactorEntity.class));
    }
}
